import { defineStore } from 'pinia'
import {ref, watch} from 'vue'
import { login,getUserInfo } from '@/api/base.js'
import { myRouter } from '../../utils/permission'

export const useUserStore = defineStore('user', () => {
    const userInfo = ref({
        nickname: '',
        username: ''
    })
    const token = ref(uni.getStorageSync('x-token') || '')

    const setToken = (val) => {
        token.value = val
    }
    const setUserInfo = (val) => {
        userInfo.value = val
    }
	

    // 登录
    const loginIn = async (loginInfo) => {
        const res = await login(loginInfo)
        if(res.code === 0) {
            setToken(res.data.token)
            return true
        }
        uni.showToast({	// 提示错误信息
            icon:'none',
            title: res.msg,
            duration: 3000
        })
        return false
    }

    const loginOut = async() =>{
        uni.removeStorageSync("x-token")
        clearUserInfo()
		myRouter('/pages/home/index',true)
    }

	const getInfo = async ()=> {
		const token = uni.getStorageSync('x-token')
		if(token){
			const infoRes = await getUserInfo()
			if(infoRes.code === 0){
				await setUserInfo(infoRes.data)
			}
		}
	}
	
    // 清空用户信息
    const clearUserInfo = () => {
        setUserInfo(
            {
                nickname: '',
                username: ''
            }
        )
        setToken("")
    }

    watch(()=>token.value,()=>{
        uni.setStorageSync('x-token',token.value)
		if(token.value){
			getInfo()
		}else{
			clearUserInfo()
		}
    })

    return {
        userInfo,
        token,
        loginIn,
        setToken,
        setUserInfo,
        getInfo,
        clearUserInfo,
        loginOut
    }
})